#include <Servo.h>

Servo servo1;


void setup() {

  pinMode(9,OUTPUT);
//  servo1.attach(9); 
  //servo1.setMaximumPulse(2000);
  //servo1.setMinimumPulse(700);
  // REAL maximum ange: 130 deg
  servo1.attach(9,800,2700);  // attaches the servo on pin 9 to the servo object
// START AT 800 for 0 = 0
// END AT 2600 for max at 140deg
// END at 2500 for max 148deg
// END at 2800 for max 127deg
// END at 2700 for max 132deg
// Real angle max: 

  Serial.begin(115200);
  Serial.println("Ready");

}

void loop() {

  static int v = 0;

  if ( Serial.available()) {
    char ch = Serial.read();

    switch(ch) {
      case '0'...'9':
        v = v * 10 + ch - '0';
        break;
      case 's':
        servo1.write(v);
        v = 0;
        break;
    }
  }


}


/*#include <Servo.h>

Servo myservo;  // create servo object to control a servo
                // a maximum of eight servo objects can be created

int pos = 0;    // variable to store the servo position

void setup()
{
	Serial.begin(115200);
  //myservo.attach(9,900,2100);  // attaches the servo on pin 9 to the servo object
  myservo.attach(9,400,2600);  // attaches the servo on pin 9 to the servo object

  //myservo.attach(9);
}


void loop()
{
  for(pos = 0; pos < 164; pos += 1)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    Serial.println(pos);
	if(pos> 150) delay(100);
	if(pos> 160) delay(250);


    delay(25);                       // waits 15ms for the servo to reach the position
  }
  for(pos = 164; pos>=0; pos-=1)     // goes from 180 degrees to 0 degrees
  {
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    Serial.println(pos);
	if(pos< 30) delay(100);
	if(pos< 10) delay(250);
    delay(25);                       // waits 15ms for the servo to reach the position

  }
}

*/